package com.shenmazg6.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazg6.pojo.TbWare;
import com.shenmazg6.service.TbWareService;
import com.shenmazg6.mapper.TbWareMapper;
import com.shenmazg6.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Think
* @description 针对表【tb_ware(产品库存表)】的数据库操作Service实现
* @createDate 2022-12-20 10:54:00
*/
@Service
public class TbWareServiceImpl extends ServiceImpl<TbWareMapper, TbWare>
    implements TbWareService{

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public ResultResponse sync() {

        // 检查索引是否存在
        if(!elasticsearchRestTemplate.indexOps(TbWare.class).exists()) {
            // 创建索引
            elasticsearchRestTemplate.indexOps(TbWare.class).create();

            // 手动创建映射
            Document mapping = elasticsearchRestTemplate.indexOps(TbWare.class).createMapping();
            elasticsearchRestTemplate.indexOps(TbWare.class).putMapping(mapping);
        }

        // 全量的数据同步
        List<TbWare> list = list();
        elasticsearchRestTemplate.save(list);

        return ResultResponse.SUCCESS();
    }
}




